<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>获取父元素</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        ul,li {
            list-style: none;
        }
    </style>
</head>

<body>
    <header>
        <nav>
            <ul>
                <li>
                    <a href="https://www.eveningwater.com" target="_blank" rel="noopener noreferrer" id="home-link"></a>
                </li>
            </ul>
        </nav>
    </header>
    <div id="content"></div>
    <script>
        const getParentsUntil = (el, selector) => {
            const parents = [];
            let _el = el.parentNode;
            while (_el && typeof _el.matches === 'function') {
                parents.unshift(_el)
                if (_el.matches(selector)) {
                    return parents
                } else {
                    _el = _el.parentNode;
                }
            }
            return parents;
        }
        const items = getParentsUntil(document.querySelector('#home-link'), 'header').map(item => item?.nodeName?.toLowerCase())
        document.querySelector('#content').innerHTML = JSON.stringify(items)
    </script>
</body>

</html>